﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Get_Article_Goods_InfoController : UserController
    {
        public IArticle_goodsService _article_goods { get; set; }
        public IUser_group_priceService _user_group_price { get; set; }
        public IUser_groupsService _user_groups { get; set; }
        [HttpGet]
        public IActionResult Get(int article_id, string spec_ids)
        {
            int channel_id = 2;
            if (channel_id == 0 || article_id == 0)
            {
                return Ok(new { status = 0, msg = "传输参数不正确" });
            }
            if (string.IsNullOrEmpty(spec_ids))
            {
                return Ok(new { status = 0, msg = "传输规格参数不正确" });
            }
            //查询商品信息
            Article_goods goodsModel = _article_goods.GetModel(article_id, spec_ids);
            if (goodsModel == null)
            {
                return Ok(new { status = 0, msg = "暂无查到商品信息" });
            }
            //查询是否登录，有则查询会员价格
            Users userModel = GetUserInfo();
            if (userModel != null)
            {
                //查询会员组价格
                User_group_price userPriceModel = _user_group_price.GetModel(goodsModel.id, userModel.group_id);
                if (userPriceModel != null)
                {
                    goodsModel.sell_price = userPriceModel.price;
                }
                else
                {
                    //查询会员组折扣
                    User_groups groupModel = _user_groups.GetModel(userModel.group_id);
                    if (groupModel != null)
                    {
                        goodsModel.sell_price = goodsModel.sell_price * groupModel.discount / 100;
                    }
                }
            }
            //以JSON格式输出商品信息
            return Ok(new
            {
                status = 1,
                id = goodsModel.id,
                article_id = goodsModel.article_id,
                goods_id = goodsModel.id,
                goods_no = goodsModel.goods_no,
                spec_ids = goodsModel.spec_ids,
                spec_text = goodsModel.spec_text,
                stock_quantity = goodsModel.stock_quantity,
                market_price = goodsModel.market_price,
                sell_price = goodsModel.sell_price
            });
        }
    }
}